package com.wen.titledemo.app;

import android.content.Intent;
import android.os.Build;
import android.os.SystemClock;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.wen.titledemo.R;
import com.wen.titledemo.app.base.BaseActivity;
import com.wen.titledemo.app.loading.LoadingManager;
import com.wen.titledemo.app.loading.OnLoadingListener;
import com.wen.titledemo.app.title.DefBarBuild;
import com.wen.titledemo.app.title.DefTitleBar;
import com.wen.titledemo.app.title.TitleBarUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;


/**
 * AppActivity 应用的Activity
 */

public abstract class AppActivity extends BaseActivity {
    /**
     * 自定义标题
     */
    protected DefTitleBar mTitleBar;
    /**
     * 加载页面
     */
    protected LoadingManager mLoadingLayout;
    /**
     * ButterKnife注解
     */
    // TODO 可以自已添加 ButterKnife
    //private Unbinder mBinder;
    /**
     * 退出应用的操作
     */
    private long mHits[] = new long[2];


    @Override
    protected void beforeCreate() {

    }


    @Override
    protected final void initTitle() {

        if (isNeedTitle()) {
            mTitleBar = new DefBarBuild(this, getTitleParent())
                    // TODO 左边返回的按钮
                    .setLeftRes(R.mipmap.frame_ic_title_back)
                    .setBgColor(getResources().getColor(R.color.colorPrimary))
                    .build();
            initTitle(mTitleBar);
            // 兼容 4.4 状态栏 && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                TitleBarUtil.setStatusTranslucent(this);
                TitleBarUtil.setTitlePadding(this, mTitleBar.getTitleBar());
            }
        }
    }

    /**
     * 加载布局作用在哪个View上
     */
    protected Object getLoadingParentView() {
        return mContentView;
    }

    private class AppLoadingListener extends OnLoadingListener {

        @Override
        public void setRetryEvent(View retryView) {
            setViewClick(retryView);
        }

        @Override
        public void setDataErrorEvent(View errorDataView) {
            setViewClick(errorDataView);
        }

        @Override
        public int generateEmptyLayoutId() {
            int emptyLayoutId = getEmptyLayoutId();
            if (emptyLayoutId != -1) {
                return emptyLayoutId;
            }
            return super.generateEmptyLayoutId();
        }

        @Override
        public void setEmptyEvent(View emptyView) {
            setEmptyEventClick(emptyView);
        }
    }

    /**
     * 默认加载页面的监听
     */
    protected OnLoadingListener getOnLoadingListener() {
        return new AppLoadingListener();
    }

    /**
     * 网络失败点击重新调用
     */
    protected void onRetryClick() {
        initNet();
    }

    /**
     * 空页面处理事件
     *
     * @param emptyView
     */
    protected void setEmptyEventClick(View emptyView) {
    }

    /**
     * 空布局
     */
    protected int getEmptyLayoutId() {
        return -1;
    }

    /**
     * 设置错误View的点击
     *
     * @param retryView
     */
    private void setViewClick(View retryView) {
        if (retryView != null) {
            retryView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRetryClick();
                }
            });
        }
    }


    /**
     * 初始化设置Title
     */
    protected abstract void initTitle(DefTitleBar titleBar);

    /**
     * Title作用在哪个View上
     */
    protected ViewGroup getTitleParent() {
        return null;
    }

    /**
     * 是否有Title，默认是有的
     *
     * @return
     */
    protected boolean isNeedTitle() {
        return true;
    }

    @Override
    protected final void bindView(View contentView) {
        // TODO 可以自已添加 ButterKnife
        //mBinder = ButterKnife.bind(this, contentView);
        EventBus.getDefault().register(this);
        // 初始化加载页面
        mLoadingLayout = LoadingManager.create(getLoadingParentView(), getOnLoadingListener());
        mLoadingLayout.showContent();
    }

    @Override
    protected final void unbindView(View contentView) {
        EventBus.getDefault().unregister(this);
        // TODO 可以自已添加 ButterKnife
//        if (mBinder != null) {
//            mBinder.unbind();
//            mBinder = null;
//        }
        mLoadingLayout = null;
        mTitleBar = null;
    }

    /**
     * 防止EventBus报错
     */
    @Subscribe
    public void onEvent(AppActivity activity) {

    }


    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        this.overridePendingTransition(R.anim.frame_push_right_in, R.anim.frame_push_right_out);
    }

    /**
     * 关闭页面
     *
     * @param isAnimation
     */
    public void finish(boolean isAnimation) {
        super.finish();
        if (isAnimation) {
            this.overridePendingTransition(R.anim.frame_push_left_in, R.anim.frame_push_left_out);
        }
    }

    public void finish() {
        finish(true);
    }


    @Override
    public void onBackPressed() {
        if (isExit()) {
            // 判断帮助提示有没有显示
            System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
            mHits[mHits.length - 1] = SystemClock.uptimeMillis();
            if (mHits[0] >= (SystemClock.uptimeMillis() - 2000)) {
                //TSUtil.cancel();
                finish(false);
            } else {
                Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
                //TSUtil.show("再按一次退出应用");
            }
        }
    }

    /**
     * 是否要退出
     */
    protected boolean isExit() {
        return false;
    }


}
